﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using TierApplication;
using System.Data;

public partial class ForeProgram_controls_Header : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            if (Request.QueryString["Bandwidth"] != null)
            {
                ViewState["type"] = "Bandwidth";
                ViewState["cond"] = Request.QueryString["Bandwidth"];
            }
            if (Request.QueryString["LifeTime"] != null)
            {
                ViewState["type"] = "LifeTime";
                ViewState["cond"] = Request.QueryString["LifeTime"];
            }
            if (Request.QueryString["CategoryId"] != null)
            {
                ViewState["type"] = "CategoryId";
                ViewState["cond"] = Request.QueryString["CategoryId"];
            }
            if (Request.QueryString["keyWord"] != null)
            {
                txtKeyWord.Text = Request.QueryString["keyWord"].ToString();
            }
            LoadMenu();
            LoadProduct();
            lblName.Text = Security.GetAccountName();

        }
    }

    #region 加载顶菜单
    /// <summary>
    /// 加载顶菜单
    /// </summary>
    private void LoadMenu()
    {
        StringBuilder sb = new StringBuilder("");
        P_DDCODE code = new P_DDCODE();
        string css = " class='actived' ";
        string cond = string.Empty;
        DataSet list = code.GetList(" ddtype='Bandwidth'");
        if (ViewState["type"] != null && ViewState["type"] == "Bandwidth")
        {
            sb.Append(@"<a id='Bandwidth_' href='product.aspx'  >首页</a> ");
            cond = ViewState["cond"].ToString();
        }
        else
        {
            sb.Append(@"<a id='Bandwidth_' href='product.aspx' " + css + " >首页</a> ");
        }
        if (list != null && list.Tables.Count != 0 && list.Tables[0].Rows.Count != 0)
        {
            for (int i = 0; i < list.Tables[0].Rows.Count; i++)
            {
                string Key = "Bandwidth_" + list.Tables[0].Rows[i]["codeid"].ToString();
                if (cond != "" && cond.Equals(list.Tables[0].Rows[i]["codeid"].ToString()))
                {
                    sb.Append(@"<a id='" + Key + "' href='product.aspx?Bandwidth=" + list.Tables[0].Rows[i]["codeid"].ToString() + "'" + css + ">" + list.Tables[0].Rows[i]["codename"].ToString() + "</a> ");
                }
                else
                {
                    sb.Append(@"<a id='" + Key + "' href='product.aspx?Bandwidth=" + list.Tables[0].Rows[i]["codeid"].ToString() + "'>" + list.Tables[0].Rows[i]["codename"].ToString() + "</a> ");
                }
            }
        }
        navigation.InnerHtml = sb.ToString();
    }
    #endregion
    protected void btnLoad_Click(object sender, EventArgs e)
    {

    }

    #region LoadProduct
    private void LoadProduct()
    {
        lblName.Text = Security.GetAccountName();
        string strWhere = string.Empty;
        strWhere = " And ACCOUNTID='" + Security.GetAccountId() + "'";
        T_CART cart = new T_CART();
        int Count = cart.GetProductCount(strWhere);
        lblProduct.Text = Count.ToString();
        if (Count == 0)
        {
            hlSettle.Attributes.Add("onclick", "alert('购物车为空，请添加商品')");
        }
        else
        {
            hlSettle.NavigateUrl = "../shopCartView.aspx";
        }
    }
    #endregion

    #region 查询
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgSearch_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("product.aspx?keyWord="+txtKeyWord.Text);
    }
    #endregion
}